<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
require_once( ROOT_DIR."config.inc.php" );
require_once( ROOT_DIR."uc_client/client.php" );

global $logid;
$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();

//write to database
if(isset($_POST['action'])) 
{
	$action=trim($_POST['action']);
	if($action=='account_info') {
		//use loop to ensure single entrance and single exit
		do{
			$params = array();
			//$params['id'] = intval(trim($_POST['id']));
			$params['id'] = $logid;
			
			$params['email'] = edp_db_input($_POST['email']);
			/*
			if((!edp_is_empty_string($params['email'])&&
			!preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/', $params['email'] ))||
			(strlen($params['email'])>50))
			{
				$errmsg->add( 'account_info', '错误的Email格式！', 'error' );
				break;
			}
			*/
			$userDAO=GetUserDAO();
			$uid=$userDAO->GetUserByEmail($params['email']);
			if($uid!=false&&$uid!=$params['id'])
			{
				$errmsg->add( 'account_info', '邮箱地址已经存在！', 'error' );
				break;
			}
			
			$params['name'] = edp_db_input($_POST['name']);
			/*
			if(strlen($params['name'])>18)
			{
				$errmsg->add( 'account_info', '姓名不能超过18个字符！', 'error' );
				break;
			}
			*/
			
			$params['address'] = edp_db_input($_POST['address']);
			/*
			if(strlen($params['address'])>180)
			{
				$errmsg->add( 'account_info', '地址不能超过180个字符！', 'error' );
				break;
			}*/
			
			$params['zip'] = edp_db_input($_POST['zip']);
			/*
			if( !edp_is_empty_string($params['zip']) && !preg_match('/^[0-9]{6,6}$/', $params['zip']))
			{
				$errmsg->add( 'detail_info', '邮政编码格式错误！', 'error' );
				break;
			}
			*/
			
			$params['phone'] = edp_db_input($_POST['phone']);
			/*
			if((!edp_is_empty_string($params['phone']) && !preg_match('/^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/', $params['phone']))||
			strlen($params['phone'])>18)
			{
				$errmsg->add( 'detail_info', '电话号码格式错误！', 'error' );
				break;
			}*/
			
			$params['oldpwd'] = edp_db_input($_POST['oldpwd']);
			if( !edp_is_empty_string($params['oldpwd']) && !$userDao->IsPasswordCorrect($params['id'], $params['oldpwd'] ) )
			{
				$errmsg->add( 'account_info', '旧密码不正确', 'error' );
				break;
			}
			$params['newpwd'] = edp_db_input($_POST['newpwd']);
			$params['newpwdagain'] = edp_db_input($_POST['newpwdagain']);
			if( $params['newpwd'] != $params['newpwdagain'] )
			{
				$errmsg->add( 'account_info', '两次新密码输入不一致', 'error' );
				break;
			}
				
			$pageVars = array_merge( $pageVars, $params );
			// when there is error, break the loop
			if( IsParaOK() == false || $errmsg->size('account_info') > 0 )
			{
				break;
			}

			if( !$userDao->UpdateUserAccountInfo( $params ) ) {
				$errmsg->add( 'account_info', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'account_info', '更新成功', 'success' );
				//update user info to ucenter for other system to login
				$userInfo = getUserFromCookie();
				uc_user_edit( $userInfo['username'], $params['oldpwd'], $params['newpwd'] );
			}

			break;
		}while(1);
	}
	else{
		$errmsg->add( 'account_info', '未知操作', 'error' );
	}
}
//	$userInfo = getUserFromCookie();
//
//	if( $userInfo ){
		//get user basic_info from database
		$userAccountInfo = $userDao->GetUserAccountInfo( $logid );

		if( $userAccountInfo ){
			$pageVars['id'] = $userAccountInfo['id'];
			$pageVars['phone'] = $userAccountInfo['phone'];
			$pageVars['name'] = $userAccountInfo['name'];
			$pageVars['address'] = $userAccountInfo['address'];
			$pageVars['zip'] = $userAccountInfo['zip'];
			$pageVars['pwd'] = $userAccountInfo['pwd'];
			$pageVars['email'] = $userAccountInfo['email'];
		}
		else{
			$errmsg->add( 'account_info', '读取用户信息失败', 'error' );
		}
//	}
//	else{
//		$errmsg->add( 'account_info', '登录用户信息不合法', 'error' );
//	}
?>
